//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年3月18日   下午10:27:57
 */
package edu.chinasoft.handler;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.chinasoft.domain.User;
import edu.chinasoft.service.UserService;
@Controller("UserHandler")
@RequestMapping("/userhandler")
public class UserHandler {
	private Logger logger = LoggerFactory.getLogger(UserHandler.class);
	private UserService UserService;
	//处理登录请求
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public void method01(HttpServletRequest request,HttpServletResponse response) {
		// TODO Auto-generated method stub
		logger.debug("UserHandler>>>method01 start ……");
		try {
			//获取页面提交的内容
			String userName = request.getParameter("user_name");
			String userPassword = request.getParameter("user_password");
			//封装到javabean
			User user = new User();
			user.setUserName(userName);
			user.setUserPasswrod(userPassword);
			//调用服务层方法
			User loginUser = this.UserService.loginUser(user);
			logger.debug("UserHandler>>>method01 end ……");
			if(loginUser==null) {
				//根据执行结果实现页面跳转
				response.sendRedirect(""+request.getContextPath()+"/login_fail.jsp");
			}else {
				//把登录的用户保存到域里
				HttpSession session = request.getSession();
				session.setAttribute("user", loginUser);
				//跳转到主页面
				response.sendRedirect(""+request.getContextPath()+"/main.jsp");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
